package cn.com.cetccst.appshield.worker;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cn.com.cetccst.datacenter.policy.type.BlackWhiteAppSetting;
import cn.com.cetccst.mdm.MDMManager;
import cn.com.cetccst.mdm.engine.IMDMEngine;
import cn.com.cetccst.tools.log.CommonLogger;

public class CheckBlackAppsWorker extends Worker {

    public static final String DATA_CONFIG = "data_config";

    public CheckBlackAppsWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        CommonLogger.info("checking black apps in worker");
        try {
            List<String> apps = new ArrayList<>();
            String config = getInputData().getString(DATA_CONFIG);
            BlackWhiteAppSetting setting = new Gson().fromJson(config, BlackWhiteAppSetting.class);
            if (setting != null) {
                for (BlackWhiteAppSetting.App app : setting.getApps()) {
                    apps.add(app.getPackageName());
                }
                IMDMEngine engine = MDMManager.getInstance().getMdmEngine();
                engine.setDisallowRunningApps(apps, setting.isBlackWhiteType());
            }
            //表示任务执行成功
            return Result.success();
        } catch (Exception e) {
            //表示任务执行失败
            return Result.retry();
        }
    }
}
